package com.example.ui.leftmenu;

import java.util.ArrayList;

import com.example.huashangapp.BaseView;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.service.MainService;
import com.example.ui.learn.CurriculumCenterActivity;
import com.example.ui.learn.DetailActivity;
import com.example.ui.learn.InviteActivity;
import com.example.ui.learn.RecommendActivity;
import com.example.ui.learn.StartActivity;
import com.example.ui.learn.util.MyClassroom;
import com.example.ui.learn.util.MyListViewAdapter3;
import com.example.ui.learn.util.MyListViewAdapter5;
import com.example.ui.learn.util.Recommend;

import com.example.util.AsyncLoadImage;
import com.example.util.RefreshableListView;
import com.example.util.RefreshableListView.OnRefreshListener;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @Description 选项3
 */
public class Item03View extends BaseView {
	private Context context;
	HSApplication hsApplication;
	private Button ra_activity, ra_course;
	
	private String data;
	private ArrayList<Recommend> ra_list= new ArrayList<Recommend>();
	private MyListViewAdapter5 ra_adapter;
	private Button titleLeftBtn, titleRightBtn;
	private TextView titleText;	
	private RefreshableListView mListView;
	private int type=0; //0courese  1activity
	AlertDialog infoad;
	AlertDialog.Builder bulider;
	Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				infoad.dismiss();
				ra_adapter.setCcInfos(ra_list);
				ra_adapter.notifyDataSetChanged();
				break;
			default:
				break;
			}
		};
	};

	public Item03View(Context context, HSApplication hsApplication) {
		this.context = context;
		this.hsApplication=hsApplication;
		HSApplication.ra_list.put("0",ra_list);
		HSApplication.ra_list.put("1",ra_list);
	}


	private Runnable allActivity_run = new Runnable() {
		@Override
		public void run() {
			data = NetRequest.Request_allActivity(type);		
			ra_list = AnalyzingJson.getdata_ra(data);
			HSApplication.ra_list.put(type+"",ra_list);
			h.sendEmptyMessage(1);
		}
	};
	public void init() {
		view = LayoutInflater.from(context).inflate(R.layout.a_recommend, null);
		titleLeftBtn = (Button) view.findViewById(R.id.layout_title_left_btn);
		titleRightBtn = (Button) view.findViewById(R.id.layout_title_right_btn);
		titleText = (TextView) view.findViewById(R.id.layout_title_middle_text);
		titleLeftBtn.setText("菜单");
		titleText.setText("线下课程活动");
		titleRightBtn.setText("发起");
		ra_activity = (Button) view.findViewById(R.id.ar_activity);
		ra_course = (Button) view.findViewById(R.id.ar_course);
		ra_course.setBackgroundResource(R.drawable.down_select);
		ra_activity.setBackgroundResource(R.color.transparent);
		mListView=(RefreshableListView)view.findViewById(R.id.item01_listview);		
		ra_adapter = new MyListViewAdapter5(context, ra_list,
				new AsyncLoadImage());	
		 mListView.setAdapter(ra_adapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int position,
						long arg3) {
					String id=ra_list.get(position).getId();
					String bId=ra_list.get(position).getTeacherId();
					Intent i=new Intent();
					i.setClass(context, DetailActivity.class);
					i.putExtra("id", id);
					i.putExtra("bId", bId);
					i.putExtra("type", type);
					context.startActivity(i);
				}
			});
			mListView.setOnRefreshListener(new OnRefreshListener() {
				@Override
				public void onRefresh(RefreshableListView listView) {
					new NewDataTask().execute();
				}
			});
		titleLeftBtn.setOnClickListener(ra_ocl);
		titleRightBtn.setOnClickListener(ra_ocl);
		ra_activity.setOnClickListener(ra_ocl);
		ra_course.setOnClickListener(ra_ocl);
		//初始化数据
		getdata();
	}
	private class NewDataTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {}
            
			return null;
        }

        @Override
        protected void onPostExecute(String result) {
            
        	bulider = new AlertDialog.Builder(context);
			bulider.setView(View.inflate(context, R.layout.onloading, null))
					.create();
			infoad = bulider.show();
			new Thread(allActivity_run).start();
            mListView.completeRefreshing();
        	

            super.onPostExecute(result);
        }
    }
	private void getdata() {
		if(HSApplication.ra_list.size()>0&&HSApplication.ra_list.get(type+"").size()>0){
			ra_list=HSApplication.ra_list.get(type+"");
			ra_adapter.setCcInfos(ra_list);
			ra_adapter.notifyDataSetChanged();
		}else{
			bulider = new AlertDialog.Builder(context);
			bulider.setView(View.inflate(context, R.layout.onloading, null))
					.create();
			infoad = bulider.show();
			new Thread(allActivity_run).start();
		}
	}
    private void runHander(){   
    	
			getdata();
    }
	
	private OnClickListener ra_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {
		
			switch (v.getId()) {
			case R.id.ar_activity:
				type=1;
				getdata();
				ra_activity.setBackgroundResource(R.drawable.down_select);
				ra_course.setBackgroundResource(R.color.transparent);
				break;
			case R.id.ar_course:
				type=0;
				runHander();
				ra_course.setBackgroundResource(R.drawable.down_select);
				ra_activity.setBackgroundResource(R.color.transparent);
				break;
			case R.id.layout_title_left_btn:
				myMoveView.showHideLeftMenu();
				break;
			case R.id.layout_title_right_btn:
				Intent intent = new Intent(context, StartActivity.class);
				context.startActivity(intent);
				break;
			default:
				break;
			}
		}
	};
}
